﻿using System.Collections.Generic;
using System.Globalization;

namespace MoBaoPay.Model.Request
{
    /// <summary>
    /// 资金待发
    /// </summary>
    internal class MoBaoFundOutRequest : IMoBaoRequest
    {
        /// <summary>
        /// 接口名字
        /// </summary>
        public string ApiName { get; set; } = "FUND_OUT";

        /// <summary>
        /// 商户号
        /// </summary>
        public string CustNo { get; set; }

        /// <summary>
        /// 商户订单号
        /// </summary>
        public string MerchTradeNo { get; set; }

        /// <summary>
        /// 收款账户类型
        /// </summary>
        public int AccInType { get; set; } = 2;

        /// <summary>
        /// 银行卡号
        /// </summary>
        public string AccInNo { get; set; }
        
        /// <summary>
        /// 银行卡名称
        /// </summary>
        public string AccInName { get; set; }

        /// <summary>
        /// 交易金额
        /// </summary>
        public decimal TradeAmt { get; set; }

        /// <summary>
        /// 描述交易概要
        /// </summary>
        public string TradeSummer { get; set; }
        
        /// <summary>
        /// 后台通知Url
        /// </summary>
        public string MerchUrl { get; set; }

        /// <summary>
        /// 支付方式，资金代发
        /// </summary>
        public int PayType { get; set; } = 314;

        /// <summary>
        /// 交易来源
        /// </summary>
        public int TerminalType { get; set; } = 0;

        /// <summary>
        /// 发起请求IP
        /// </summary>
        public string TerminalId { get; set; }
        
        /// <summary>
        /// 签名
        /// </summary>
        public string SignMsg { get; set; }

        private string requestUrl = "https://trade2.mobaopay.com/cashier/api/gateway.cgi";

        public string GetRequestUrl()
        {
            return requestUrl;
        }

        public void SetRequestUrl(string url)
        {
            requestUrl = url;
        }

        public IDictionary<string, string> GetRequestContent()
        {
            return new Dictionary<string, string>
            {
                { "accInName", AccInName },
                { "accInNo", AccInNo },
                { "accInType", AccInType.ToString() },
                { "apiName", ApiName },
                { "custNo", CustNo },
                { "merchTradeNo", MerchTradeNo },
                { "merchUrl", MerchUrl },
                { "payType", PayType.ToString() },
                { "terminalId", TerminalId },
                { "terminalType", TerminalType.ToString() },
                { "tradeAmt", TradeAmt.ToString(CultureInfo.InvariantCulture) },
                { "tradeSummer", TradeSummer },
                { "signMsg", SignMsg }
            };
        }

        public void Sign(string path, string password)
        {
            var paras = GetRequestContent();
            var strData = string.Empty;
            foreach (var para in paras)
            {
                if (string.IsNullOrWhiteSpace(para.Value))
                {
                    continue;
                }

                if (string.IsNullOrWhiteSpace(strData))
                {
                    strData += $"{para.Key}={para.Value}";
                }
                else
                {
                    strData += $"&{para.Key}={para.Value}";
                }
            }


            this.SignMsg = MD5WithRSA.SignData(path, password, strData);
        }
    }
}